package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;

import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * npt011保费试算
 *
 * @author : 追到乌云的尽头找太阳-（Jacob）
 * @date : 2020/4/3 16:35
 **/
public class Npt011TrialStrategy implements TrialStrategy {

    private static final Logger logger = LoggerFactory.getLogger(Npt011TrialStrategy.class);

    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                TrialPersonModel trialPersonModel = new TrialPersonModel();
                String riskCode = productModel.getProdCode();
                trialPersonModel.setRiskCode(riskCode);
                StringBuilder sb = new StringBuilder(16);
//                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
//                Object object = redisService.get(sb.toString());
//                if (object == null) {
//                    return new TrialPersonModel();
//                }
//                TrialUtil.setSpecialElement(trialPersonModel, object.toString());
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|").append(trialPersonModel.getRiskCode()).append("|G");
                Object object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                trialPersonModel.setFormula(object.toString());
                logger.info("【" + productModel.getProdCode() + "】【公式】【KEY】" + sb.toString());
                trialPersonModel.setPrem(productModel.getPrem());
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                logger.info("【" + riskCode + "】【产品计算公式要素配置】【KEY】" + sb.toString());
                TrialUtil.setElementMapping(trialPersonModel, object.toString());
                return trialPersonModel;
            }
        }
        return new TrialPersonModel();
    }
}
